package com.zhongkun.datahome.service.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongkun.datahome.service.domain.CustomerBase;
import com.zhongkun.datahome.service.domain.CustomerHouse;
import com.zhongkun.datahome.service.domain.HouseServerRelation;
import com.zhongkun.datahome.service.mapper.CustomerHouseMapper;
import com.zhongkun.datahome.service.processor.dto.HouseMaterialDto;
import com.zhongkun.datahome.service.req.qry.house.CustomerHouseQry;
import com.zhongkun.datahome.service.res.house.CustomerHouseVo;
import com.zhongkun.datahome.service.utils.StreamUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/14 17:15
 * @Desc
 */
@Repository
public class CustomerHouseRepository extends ServiceImpl<CustomerHouseMapper, CustomerHouse> {
    @Resource
    private CustomerBaseRepository customerBaseRepository;

    @Resource
    private HouseServerRelationRepository houseServerRelationRepository;

    public List<CustomerHouse> list(String roomNum) {

        return this.list(new LambdaQueryWrapper<CustomerHouse>()
                .like(StringUtils.isNotBlank(roomNum), CustomerHouse::getRoomNum, roomNum)
                .orderByDesc(CustomerHouse::getId));
    }

    public List<CustomerHouse> listByFamilyId(Long familyId) {

        return this.list(new LambdaQueryWrapper<CustomerHouse>()
                .eq( CustomerHouse::getFamilyId, familyId));
    }

    public List<CustomerHouse> listHouseByServerTenantUserId(Long serverTenantUserId, String roomNum) {
        List<HouseServerRelation> list = houseServerRelationRepository.list(new LambdaQueryWrapper<HouseServerRelation>()
                .select(HouseServerRelation::getHouseId)
                .eq(HouseServerRelation::getServerUserId, serverTenantUserId));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        List<Long> houseIds = list.stream().map(HouseServerRelation::getHouseId).collect(Collectors.toList());
        return this.list(new LambdaQueryWrapper<CustomerHouse>()
                .in(CustomerHouse::getId, houseIds)
                .like(StringUtils.isNotBlank(roomNum), CustomerHouse::getRoomNum, roomNum)
                .orderByDesc(CustomerHouse::getId));
    }

    public List<CustomerHouse> listByTenantUserId(Long tenantUserId) {
        CustomerBase customerBase = customerBaseRepository.getOne(new LambdaQueryWrapper<CustomerBase>()
                .select(CustomerBase::getFamilyId)
                .eq(CustomerBase::getTenantUserId, tenantUserId), false);
        if (Objects.isNull(customerBase)) {
            return Collections.emptyList();
        }

        return this.list(new LambdaQueryWrapper<CustomerHouse>()
                .eq(CustomerHouse::getFamilyId, customerBase.getFamilyId())
                .orderByDesc(CustomerHouse::getId));
    }

    public IPage<CustomerHouseVo> pageCustomerHouseVo(CustomerHouseQry qry) {
        IPage<CustomerHouseVo> page = new Page<>(qry.getPageNo(), qry.getPageSize());
        return getBaseMapper().page(page, qry);
    }

    public IPage<HouseMaterialDto> pageHouseMaterialDto(CustomerHouseQry qry) {
        IPage<HouseMaterialDto> page = new Page<>(qry.getPageNo(), qry.getPageSize());
        return getBaseMapper().pageMaterialList(page, qry);
    }

    public List<CustomerHouse> getLike(String buildingNum, String roomNum) {
        return this.list(new LambdaQueryWrapper<CustomerHouse>()
                .like(StringUtils.isNotBlank(buildingNum), CustomerHouse::getBuildingNum, buildingNum)
                .like(StringUtils.isNotBlank(roomNum), CustomerHouse::getRoomNum, roomNum));
    }

    public CustomerHouse checkHouseRoomExist(String buildingNum, String roomNum, Long id) {
        return this.getOne(new LambdaQueryWrapper<CustomerHouse>()
                .ne(id != null, CustomerHouse::getId, id)
                .eq(CustomerHouse::getBuildingNum, buildingNum)
                .eq(CustomerHouse::getRoomNum, roomNum), false);
    }

    public Set<String> getHouseKeyNames(List<Long> houseIds) {
        if (CollectionUtils.isEmpty(houseIds)) {
            return Collections.emptySet();
        }

        return this.list(new LambdaQueryWrapper<CustomerHouse>()
                .select(CustomerHouse::getBuildingNum, CustomerHouse::getRoomNum)
                .in(CustomerHouse::getId, houseIds))
                .stream()
                .map(item -> item.getBuildingNum() + "-" + item.getRoomNum())
                .collect(Collectors.toSet());
    }

    public List<CustomerHouse> listByHouseIdList(List<Long> customerHouseIdList) {
        return lambdaQuery().in(CustomerHouse::getId, customerHouseIdList).list();
    }
}
